<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>列表渲染</title>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
</head>

<body>
  <div id="app">
    <input v-model="inputValue" type="text" />
    <button @click="submitBtnClicked">提交</button>
    <ul>
      <todoitem v-for="(item,index) in list" :index="index" :message="item" @delete="deleteItem">{{item}}</todoitem>

    </ul>
  </div>

  <script>
    Vue.component('todoitem', {
      props: ['message', 'index'],

      //   data: function () {
      //     return {

      //     }
      //   },
      template: ' <li @click="incrementHandler">{{index}}:{{message}}</li>',

      methods: {

        incrementHandler: function () {

          console.log(this.message);
          console.log(this.index);
          this.$emit('delete', this.index);
        }
      },
    });


    var vm = new Vue({
      el: "#app",
      data: {
        list: [],
        inputValue: ""
      },
      methods: {
        submitBtnClicked: function () {
          console.log("提交");
          //   console.log(inputValue);
          this.list.push(this.inputValue);
          // this.inputValue='';
        },
        deleteItem: function (index) {
          console.log(index);
          this.list.splice(index, 1);

        },
      }
    });

  </script>
</body>

</html>
